<template>
  <div>
    <el-container style="height: 100%; border: 1px solid #eee">
      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <el-button type="primary" size="small" style="margin-left: 1000px " @click="batchApproveCheckOut">批量通过</el-button>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%"
              stripe
              border
              max-height="575"
              @selection-change="handleSelectionChange">
            <el-table-column
                type="selection"
                width="50">
            </el-table-column>
            <el-table-column
                fixed
                prop="createdTime"
                label="申请时间"
                :formatter="formatDate">
            </el-table-column>
            <el-table-column
                prop="studentName"
                label="姓名">
            </el-table-column>
            <el-table-column
                prop="areaName"
                label="园区">
            </el-table-column>
            <el-table-column
                prop="outRoomBuildingName"
                label="退宿楼栋">
            </el-table-column>
            <el-table-column
                prop="outRoomName"
                label="退宿房间">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="80">
              <template slot-scope="scope">
                <el-button type="text" size="small" style="margin-left: 0px" @click="approveCheckOutApplication(scope.row)">通过</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <div class="block">
            <el-pagination
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total=totalNum>
            </el-pagination>
          </div>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import {getCheckOutApplication, UpdateCheckOutApplication} from '../../../../../api/leader'

export default {
  data () {
    return {
      pageSize: 5,
      totalNum: 0,
      pageNo: 1,
      DOT: {
        pageSize: 5,
        pageNo: 1,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
      },
      tableData: [],
      multipleSelection: []
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.createdTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    handleCurrentChange (newPage) {
      this.pageNo = newPage
      this.DOT.pageNo = newPage
      this.queryCheckOutApplication()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    queryCheckOutApplication () {
      getCheckOutApplication(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    approveCheckOutApplication (row) {
      let obj = {
        ID: row.id.toString(),
        leaderID: JSON.parse(localStorage.getItem('user')).id.toString()
      }
      UpdateCheckOutApplication(obj).then(res => {
        if (res.status === true) {
          this.queryCheckOutApplication()
        } else {
          this.$message({
            message: '失败',
            type: 'error'
          })
        }
      })
    },
    batchApproveCheckOut () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请至少选择一项进行批量审批',
          type: 'warning'
        })
        return
      }
      Promise.all(this.multipleSelection.map(row => {
        let obj = {
          ID: row.id.toString(),
          leaderID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
        UpdateCheckOutApplication(obj).then(res => {
          if (res.status === true) {
            this.queryCheckOutApplication()
          } else {
            this.$message({
              message: '失败',
              type: 'error'
            })
          }
        })
      }))
    }
  },
  created () {
    this.queryCheckOutApplication()
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>
